//
//  KSMenuChooseView.swift
//  Slaughter
//
//  Created by Ken on 2018/3/10.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit

class KSPageControl: UIView {
    var itemspace:CGFloat = 3
    lazy var collectionView:UICollectionView = {
        // 1.创建layout
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = itemspace
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.scrollDirection = .horizontal
        // 2.创建UICollectionView
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(PageControlCell.self, forCellWithReuseIdentifier: PageControlCell.identifier)
        
        return collectionView
    }()
    
    
    var currentPage: Int! {
        didSet {
            collectionView.reloadData()
        }
    }
     
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        collectionView.delegate = self
        collectionView.dataSource = self
        addSubview(collectionView)
        collectionView.frame = self.bounds
    }
  
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
 
    
    // MARK: - Getter And Setter
    var numberOfPage: Int! {
        didSet {
            currentPage = 0
        }
    }
    
  
}
extension KSPageControl:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
     
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  
             return numberOfPage
     }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
       
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PageControlCell.identifier, for: indexPath as IndexPath) as! PageControlCell
        
        if indexPath.row == currentPage {
            cell.bg.backgroundColor = UIColor(kshex: "#5A7AF8")
        } else {
            cell.bg.backgroundColor = UIColor(kshex: "#B1B8D3")
        }
        return cell
    
    }
 
    
    
    // 设置itemsize
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.row == currentPage {
            
            let ww =  (self.ks_width - CGFloat(numberOfPage - 1) * (itemspace + self.ks_height))
            return CGSize(width: ww, height: self.ks_height)
        } else {
            return CGSize(width: self.ks_height, height: self.ks_height)
        }
   
          
   
      
   
    }
//    // 滑动停止调用方法
//    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
//        let offSet = scrollView.contentOffset.x
//        let tempIndex = offSet / coWidth
//        self.pageControl.currentPage = Int(tempIndex)
//    }
//    
//    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
//        scrollViewDidEndDecelerating(scrollView)
//    }
    
    
}
class PageControlCell: UICollectionViewCell {
    
    var bg:UIView = {
        let v = UIView()
        return v
    }()
     
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initUtil()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUtil() {
        self.contentView.layer.masksToBounds = true
        bg.frame = self.bounds
        bg.layer.cornerRadius = self.ks_height/2
        self.contentView.addSubview(bg)
        bg.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    
    }
}
 
